<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Tools\Func;
use App\Models\AdminUser;
use App\Models\HeTong;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;

use App\Imports\FeiYongImport;

class UserController extends Controller
{
    use Func;

    public function login(Request $request)
    {

        if (!$username = trim($request->username) ?? '') {
            return $this->returnJson(1, '账号不能为空', $request->all());    
        }
            if (!$password = trim($request->password) ?? '') {
            return $this->returnJson(1, '密码不能为空', $request->all());    
        }

        if (!$user = AdminUser::where([
                'admin_name' => $username,
                'password' => $password
            ])->first()) {
            return $this->returnJson(1, '账号密码错误', $request->all());  
        }

        return $this->returnJson(0, 'success', $user);

    }

    // 合同列表
    public function heTongList(Request $request)
    {
        $keywords = trim($request->keywords);
        $data = HeTong::where(function ($query) use($keywords) {
            // 关键字
            if ($keywords) {
                $query->orWhere('name', $keywords);
                $query->orWhere('no', $keywords);
            }
        })->paginate($request->limit ?? 10);
        return $this->returnJson(0, 'success', $data);
    }

    // 添加合同
    public function addHeTong(Request $request)
    {
        if (!$heTong = HeTong::create([
            'name' => $request->name,
            'no' => $request->no,
            // 'diqu' => $request->diqu,
			'password' => $request->password,
            'hetongbeianhao' => $request->hetongbeianhao,
            'beianxiangmumingcheng' => $request->beianxiangmumingcheng,
            'loudonghao' => $request->loudonghao,
            'fangjianhao' => $request->fangjianhao,
            'beianshijian' => $request->beianshijian,
			'budongchanquanzhenghao' => $request->budongchanquanzhenghao,
			'budongchandanyuanhao' => $request->budongchandanyuanhao,
			'dengjishijian' => $request->dengjishijian,
			'luozuo' => $request->luozuo,
			'mianji' => $request->mianji,
			'yongtu' => $request->yongtu,
            'quanlixingzhi' => $request->quanlixingzhi,
			'quanliren' => $request->quanliren,
			'fangwuhzuangtai' => $request->fangwuhzuangtai,           
            'created_at' => date('Y-m-d H:i:s')
        ])) {
            return $this->returnJson(1, '添加失败', $heTong);
        }

        return $this->returnJson(0, '添加成功', $heTong);
    }

    // 删除合同
    public function delHeTong(Request $request)
    {
        if (!HeTong::where('id', $request->id)->delete()) {
            return $this->returnJson(1, '删除失败');
        }

        return $this->returnJson(0, '删除成功');
    }

    // 获得合同信息
    public function getHeTongInfo(Request $request)
    {

        $heTong = HeTong::where('id', $request->id)->first();

        return $this->returnJson(0, 'success', $heTong);
    }

    // 编辑合同信息
    public function editHeTong(Request $request)
    {
        // dd($request->addAccount);
        if (!$heTong = HeTong::where('id', $request->id)->update([
            'name' => $request->name,
            'no' => $request->no,
            // 'diqu' => $request->diqu,
			'password' => $request->password,
            'hetongbeianhao' => $request->hetongbeianhao,
            'beianxiangmumingcheng' => $request->beianxiangmumingcheng,
            'loudonghao' => $request->loudonghao,
            'fangjianhao' => $request->fangjianhao,
            'beianshijian' => $request->beianshijian,
			'budongchanquanzhenghao' => $request->budongchanquanzhenghao,
			'budongchandanyuanhao' => $request->budongchandanyuanhao,
			'dengjishijian' => $request->dengjishijian,
			'luozuo' => $request->luozuo,
			'mianji' => $request->mianji,
			'yongtu' => $request->yongtu,
            'quanlixingzhi' => $request->quanlixingzhi,
			'quanliren' => $request->quanliren,
			'fangwuhzuangtai' => $request->fangwuhzuangtai,  
        ])) {
            return $this->returnJson(1, '修改失败', $heTong);
        }

        return $this->returnJson(0, '修改成功', $heTong);       
    }

}
